﻿using System.Collections.Generic;
using MyFloridaHouse.libSvDTO;
using System.Windows;
using System;

namespace MyFloridaHouse.libSvOpenDistricts.PDF
{
    public class HouseMapInset
    {
        public HouseMapRectSpecification rectSpecification { get; set; }
        public BoundingBoxDTO latLonInView { get; set; }  // lat/lon coordinates of inset
        public Rect insetXYRect { get; set; }  // X/Y coordinates of inset

        public List<HouseMapLayerRegions> mapLayerRegionsList { get; set; }
        public double zoomLevel { get; set; }
        public bool showBorder { get; set; }

        public bool _isRetrieved
        {
            get
            {
                foreach (var lyr in mapLayerRegionsList)
                {
                    if (!lyr.isRetrieved)
                    {
                        return false;
                    }
                }
                return true;
            }
            set {_isRetrieved = value;}

            }

        public HouseMapInset( double originWidthPercent, double originHeightPercent, double rectWidthPercent, double rectHeightPercent, BoundingBoxDTO latLonInView, double zoomLevel, bool showBorder)
        {
            this.rectSpecification = new HouseMapRectSpecification(originWidthPercent, originHeightPercent, rectWidthPercent, rectHeightPercent);
            this.latLonInView = latLonInView;
            this.zoomLevel = zoomLevel;
            this.mapLayerRegionsList = new List<HouseMapLayerRegions>();
            this.showBorder = showBorder;
        }


        public Rect DetermineInsetBoundaries(PDFPoint mapBorderOriginCursor, double mapBorderWidth, double mapBorderHeight)
        {

            // convert the inset percent-based specification into the actual X/Y rectangle to be used for this inset
            this.insetXYRect = rectSpecification.GetDrawingRect(mapBorderOriginCursor, mapBorderWidth, mapBorderHeight);


            /* 
             * calculate the lattitude coordinate for the south border, we do this because although the user specifies both 
             * northwest and southeast points for the inset, they also indirectly (through percentages) specify the X/Y corners of the inset.
             * That means the south latitude must be adjusted to match the X/Y coordinates
             * */
            double degreesOfLongitudeInRect = Math.Abs(latLonInView.southEast.longitude - latLonInView.northWest.longitude);
            double pointsOfXinRect = insetXYRect.Width;
            double pointsOfYinRect = insetXYRect.Height;
            double DegreesPerPoint = degreesOfLongitudeInRect / pointsOfXinRect;
            latLonInView.southEast.latitude = latLonInView.northWest.latitude - (DegreesPerPoint * pointsOfYinRect);
            

            return insetXYRect;
        }


    }
}
